<?php require_once('inc.config.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mismatched MySQL Tables</title>
<link href="mismatched.css" rel="stylesheet" type="text/css" />
</head>
<body class="sql">
<?php
$htmls = array(
	'missing' => array(),
	'mismatched' => array(),
	'extra' => array(),
);
$id = 0;
foreach($tables as $t => $table)
{
	++$id;
	#echo $table, "<br />";
	
	$remote = $db_remote->row("SHOW CREATE TABLE `{$database_remote}`.`{$table}`;");
	$remote['Create Table'] = isset($remote['Create Table'])?$remote['Create Table']:'';
	$remote['Create Table'] = preg_replace('/ AUTO_INCREMENT=\d+/is', '', $remote['Create Table']);
	#print_r($remote);

	$local = $db_local->row("SHOW CREATE TABLE `{$database_local}`.`{$table}`;");
	$local['Create Table'] = isset($local['Create Table'])?$local['Create Table']:'';
	$local['Create Table'] = preg_replace('/ AUTO_INCREMENT=\d+/is', '', $local['Create Table']);
	#print_r($local);
	
	if(!$local['Create Table'] && !$remote['Create Table']) continue;
	
	# Assuming, local database is always fresh.
	# We do not consider contents at this time. Database structures only.
	$sql = array(
	'missing'=>
"CREATE TABLE `{$database_remote}`.`{$table}` LIKE `{$database_local}`.`{$table}`;
INSERT INTO `{$database_remote}`.`{$table}` SELECT * FROM `{$database_local}`.`{$table}`;",
	'extra'=>
"CREATE TABLE `{$database_remote}`.`{$table}` LIKE `{$database_local}`.`{$table}`;
INSERT INTO `{$database_remote}`.`{$table}` SELECT * FROM `{$database_local}`.`{$table}`;",
	'mismatched'=>
"DROP TABLE `{$database_remote}`.`{$table}`;
CREATE TABLE `{$database_remote}`.`{$table}` LIKE `{$database_local}`.`{$table}`;
INSERT INTO `{$database_remote}`.`{$table}` SELECT * FROM `{$database_local}`.`{$table}`;",
	);
	if($local['Create Table'] && !$remote['Create Table'])
	{
		#echo $id, ': Missing: ', $table, "\r\n";
		#$htmls[] = "<li><span class='missing'>Missing</span>: {$table}</li>";
		$htmls['missing'][] = "<li><p><span class='missing'>Missing</span>: {$table}</p><code>{$sql['missing']}</code></li>";
	}
	else if(!$local['Create Table'] && $remote['Create Table'])
	{
		#echo $id, ': Extra: ', $table, "\r\n";
		#$htmls[] = "<li><span class='extra'>Extra</span>: {$table}</li>";
		#$htmls['extra'][] = "<li><span class='extra'>Extra</span>: {$table}</li>";
		$htmls['extra'][] = "<li><p><span class='extra'>Extra</span>: {$table}</p><code>{$sql['extra']}</code></li>";
		
	}
	else if($local['Create Table']!=$remote['Create Table'])
	{
		# Count the number of records in both of the tables.
		#echo $id, ': Mismatched: ', $table, "\r\n";
		#$htmls[] = "<li><span class='mismatched'>Mismatched</span>: {$table}</li>";
		#$htmls['mismatched'][] = "<li><span class='mismatched'>Mismatched</span>: {$table}</li>";
		$htmls['mismatched'][] = "<li><p><span class='mismatched'>Mismatched</span>: {$table}</p><code>{$sql['mismatched']}</code></li></li>";
		
		# Save the structures of the files
		file_put_contents("mismatched/{$table}.remote", $remote['Create Table']);
		file_put_contents("mismatched/{$table}.local", $local['Create Table']);
	}
	else
	{
		--$id; # optional?
	}
}

# Possible findings:
# 1. Extra table in local database
# 2. Extra in live  database / Missing (opposite of extra?)
# 3. Mismatched by structure
# 4. Not found anywhere - if you loaded the list of tables from a configuration

# Validates emtpy results
if(empty($htmls['mismatched'])) $htmls['mismatched'] = array('<li class="none">None</li>');
if(empty($htmls['missing'])) $htmls['missing'] = array('<li class="none;">None</li>');
if(empty($htmls['extra'])) $htmls['extra'] = array('<li class="none">None</li>');

#echo implode("\r\n", $htmls), '</ol>';
echo '<h1>Mismatched</h1><ol>', implode("\r\n", $htmls['mismatched']), '</ol>';
echo '<h1>Missing</h1><ol>', implode("\r\n", $htmls['missing']), '</ol>';
echo '<h1>Extra</h1><ol>', implode("\r\n", $htmls['extra']), '</ol>';

echo "Done <strong>{$database_remote}</strong>! Corresponding <em>.local</em> and <em>.remote</em> structure files are saved in [mismatched] folder.";
?>
</body>
</html>